package com.tanhua.appserver.controller;


import com.tanhua.appserver.exception.BusinessException;
import com.tanhua.appserver.interceptor.UserHolder;
import com.tanhua.appserver.service.UserInfoService;
import com.tanhua.appserver.service.UserService;
import com.tanhua.autoconfig.template.AipTemplate;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private AipTemplate aipTemplate;

    /**
     * 发送验证码
     * @param map 接收手机号
     * @return ok返回成功响应
     */
    @PostMapping("/login")
    public ResponseEntity send(@RequestBody Map map){

        userService.send(map);

        return ResponseEntity.ok(null);

    }

    /**
     * 登录
     * @param map 接受 手机号和验证码
     * @return 返回集合 (token 和  是否为老用户)
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map){

        Map result = userService.loginVerification(map);

        return ResponseEntity.ok(result);
    }

    /**
     * @Author: JW
     * @Description:  TODO 完善用户信息
     * @Date: 21:51 2021/12/21
     * @Param: [multipartFile, userInfo] multipartFile: 文件对象  userInfo:用户信息
     * @return: ResponseEntity ok代表完成
    **/
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) {

        userInfo.setId(UserHolder.getUserId());

        userInfoService.save(userInfo);

        return ResponseEntity.ok(null);
    }

    /**
     * @Author: JW
     * @Description:  TODO 上传头像 并校验是否为人像
     * @Date: 22:52 2021/12/21
     * @Param: [multipartFile]
     * @return: ResponseEntity
     **/
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto) throws IOException {

        //将文件中的需要的数据取出来
        InputStream inputStream = headPhoto.getInputStream();
        String filename = headPhoto.getOriginalFilename();

        //将文件url拼接
        String path = ossTemplate.upload(filename, inputStream);
        String url = "https://jw-tianhua-image.oss-cn-beijing.aliyuncs.com/" + path;

        //需要校验是否为人脸
        boolean b = aipTemplate.checkFace(url);

        //不为人脸时,则抛出自定义异常
        if (!b){
            throw new BusinessException(ErrorResult.faceError());
        }

        //同时需要将路径存储到userInfo中
        userInfoService.addHead(url);

        return ResponseEntity.ok(null);

    }

}
